package com.whfc.wxmp.api.fim.controller;

import com.whfc.common.result.*;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fim.dto.*;
import com.whfc.fim.param.FimResultEditParam;
import com.whfc.fim.param.FimSearchParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.fim.service.WxFimResultService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 智能监控报警记录
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020/5/27 14:48
 */
@RestController
@RequestMapping("/mp/api/fim/result")
public class WxFimResultController extends AbstractBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private WxFimResultService wxFimResultService;

    /**
     * 报警列表
     * @param wxUser
     * @param deptId
     * @return
     */
    @GetMapping("/list")
    public Result list(@CurrentWxUser WxUser wxUser,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value="startTime" ,required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                       @RequestParam(value="endTime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                       @RequestParam(value="state",required = false) Integer state,
                       @RequestParam(value="algCheckType",required = false) Integer algCheckType,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNum") Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取报警记录列表(按天)", RequestConetxtUtil.getParameters());
        PageData<FimResultDTO> list = wxFimResultService.list(pageNum, pageSize, deptId, state,algCheckType, startTime,endTime);
        return ResultUtil.success(list);
    }

    @GetMapping("/monthDatalist")
    public Result monthDatalist(@CurrentWxUser WxUser wxUser,
                                @RequestParam("deptId") Integer deptId,
                                @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month,
                                @RequestParam(value = "algCheckType", required = false) Integer algCheckType,
                                @RequestParam(value = "handleStatus", required = false) Integer handleStatus,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam("pageNum") Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取报警记录列表(按月)", RequestConetxtUtil.getParameters());
        PageData<FimResultDTO> list = wxFimResultService.monthDatalist(pageNum, pageSize, deptId, algCheckType, handleStatus, month);
        return ResultUtil.success(list);
    }

    @GetMapping("/detail")
    public Result detail(@CurrentWxUser WxUser wxUser,
                         @RequestParam("resultId") Integer resultId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取报警记录详情", "{\"resultId\":\"" + resultId + "\"}");
        FimResultDTO detail = wxFimResultService.detail(resultId);
        return ResultUtil.success(detail);
    }


    @GetMapping("/analysis")
    @Deprecated
    public Result analysis(@CurrentWxUser WxUser wxUser,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "报警记录分析", RequestConetxtUtil.getParameters());
        FimResultAnalysisDTO analysis = wxFimResultService.analysisRank(deptId, date);
        return ResultUtil.success(analysis);
    }

    @PostMapping("/handle")
    public Result handle(@CurrentWxUser WxUser wxUser,
                         @RequestParam("resultId") Integer resultId,
                         @RequestParam("handleResult") String handleResult,
                         @RequestParam("handleRemark") String handleRemark) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "报警记录处理", RequestConetxtUtil.getParameters());
        FimResultEditParam fimResultEditParam = new FimResultEditParam();
        fimResultEditParam.setResultId(resultId);
        fimResultEditParam.setHandleResult(handleResult);
        fimResultEditParam.setHandleRemark(handleRemark);
        fimResultEditParam.setHandleUserId(wxUser.getId());
        String name = wxUser.getName();
        if (StringUtils.isEmpty(name)) {
            name = wxUser.getNickName();
        }
        fimResultEditParam.setHandleUserName(name);
        wxFimResultService.handle(fimResultEditParam);
        return ResultUtil.success();
    }


    /**
     * 2.0 报警详情
     * @param wxUser
     * @param deptId
     * @return
     */
    @GetMapping("/analysisByDate/v2")
    public R<FimWxResultRateContDTO> analysisByDateV2(@CurrentWxUser WxUser wxUser,
                                 @RequestParam("deptId") Integer deptId,
                                  @RequestParam(value="state",required = false) Integer state,
                                  @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                  @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "智能监控报警记录分析(按时间范围)", RequestConetxtUtil.getParameters());
        FimWxResultRateContDTO fimWxResultRateContDTO = wxFimResultService.analysisByDateWX(deptId,startTime,endTime,state);
        return R.success(fimWxResultRateContDTO);
    }

    @GetMapping("/analysisByDate")
    public Result analysisByDate(@CurrentWxUser WxUser wxUser,
                                 @RequestParam("deptId") Integer deptId,
                                 @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "智能监控报警记录分析(按天)", RequestConetxtUtil.getParameters());
        ListData<FimResultRateContDTO> listData = wxFimResultService.analysisByDate(deptId, date);
        return ResultUtil.success(listData);
    }

    @GetMapping("/analysisByMonth")
    public Result analysisByMonth(@CurrentWxUser WxUser wxUser,
                                  @RequestParam("deptId") Integer deptId,
                                  @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "智能监控报警记录分析(按月)", RequestConetxtUtil.getParameters());
        FimWarnAnalysisDTO data = wxFimResultService.analysisByMonth(deptId, month);
        return ResultUtil.success(data);
    }

    @GetMapping("/getWarnNum")
    public Result getWarnNum(@CurrentWxUser WxUser wxUser,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam(value = "algCheckType", required = false) Integer algCheckType,
                             @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取报警数", RequestConetxtUtil.getParameters());
        FimWarnNumDTO data = wxFimResultService.getWarnNum(deptId, algCheckType, month);
        return ResultUtil.success(data);
    }

    /**
     * 报警类型明细数据统计【2.0.0新增】
     *
     * @param param
     * @return
     * @since 2.0.0
     */
    @GetMapping("/statistics")
    public R<FimWarnStatisticsDetailDTO> fimWarnStatisticsDetail(FimSearchParam param) {
        logger.info("报警类型明细数据统计 param:{}", getLoginUser(request).getId(), param);
        FimWarnStatisticsDetailDTO res = wxFimResultService.getFimWarnStatisticsDetail(param);
        return R.success(res);
    }

}
